package work.oujia.promotion_dict_data;

import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import lombok.Data;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public interface Setting {

    String base = "C:\\work-records\\Apple\\s-brandStorm\\";
    String data = base + "data.xlsx";
    String sqlFile = base + "out.sql";
    String sql = """
            INSERT INTO sys_dict (dict_code, dict_name, dict_type, sort_value, delete_flag, create_time, update_time) VALUES
            %s;
            """;
    String sqlItem = "('%s', '%s', '%s', 1, false, '2023-11-13 10:00:00', '2023-11-13 11:00:00'),\n";

    @Data
    class DataExcel {
        @ExcelProperty("Country")
        private String country;

        @ExcelProperty("ID")
        private String id;

        public String getInsertItem() {
            return String.format(sqlItem, id, country);
        }
    }

    class DataExcelListener implements ReadListener<DataExcel> {
        StringBuilder sb = new StringBuilder();


        @Override
        public void invoke(DataExcel dataExcel, AnalysisContext analysisContext) {
           sb.append(dataExcel.getInsertItem());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            String res = String.format(sql, sb.toString());
            FileUtil.writeString(res,sqlFile, Charset.defaultCharset());
        }
    }

}
